<template>
  <a-card :bordered="false">
    <div>
      <a-tabs v-model="activeKey" @change="handleChangeTab">
        <a-tab-pane key="notGenerated" tab="暂存中">
          <a-form layout="inline" @keyup.enter.native="searchQuery">
            <a-row :gutter="2">
              <a-col :xl="8" :lg="11" :md="12" :sm="24">
                <a-form-item label="开始日期">
                  <j-date placeholder="请选择开始日期" class="query-group-cust" v-model="startTime_begin"></j-date>
                  <span class="query-group-split-cust"></span>
                  <j-date placeholder="请选择结束日期" class="query-group-cust" v-model="startTime_end"></j-date>
                </a-form-item>
              </a-col>
              <a-col :xl="6" :lg="7" :md="8" :sm="24">
                <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
                  <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                  <a-button type="primary" @click="searchResetUnCreated" icon="reload" style="margin-left: 8px">重置</a-button>
                </span>
              </a-col>
            </a-row>
          </a-form>
          <a-button @click="handleAdd" type="primary" icon="plus" style="margin-bottom: 5px; margin-right: 5px">新增
          </a-button>
          <a-button type="primary" icon="download" @click="handleExportXls('费用申请-暂存中')">导出</a-button>
          <a-table
            ref="table"
            size="middle"
            bordered
            :rowKey="
              (record, index) => {
                return record.detailId
              }
            "
            class="j-table-force-nowrap1"
            :scroll="{ x: true }"
            :columns="columns"
            :dataSource="notGeneratedTable.dataSource"
            :pagination="notGeneratedTable.ipagination"
            :loading="notGeneratedTable.loading"
            :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
            @change="handleTableChange"
          >
            <template slot="htmlSlot" slot-scope="text">
              <div v-html="text"></div>
            </template>
            <template slot="imgSlot" slot-scope="text, record">
              <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
              <img
                v-else
                :src="getImgView(text)"
                :preview="record.id"
                height="25px"
                alt=""
                style="max-width: 80px; font-size: 12px; font-style: italic"
              />
            </template>
            <template slot="fileSlot" slot-scope="text">
              <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
              <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
                下载
              </a-button>
            </template>

            <span slot="action" slot-scope="text, record">
              <a v-if="record.status < 0" @click="handleEdit(record)">编辑</a>
              <a-divider type="vertical"></a-divider>
              <act-apply-btn
                v-show="record.status < 0"
                @success="loadData"
                :data-id="record.id"
                :serviceName="serviceName"
                :variables="record"
              ></act-apply-btn>
              <!--              <act-historic-detail-btn :data-id="record.id"></act-historic-detail-btn>-->
              <a-divider type="vertical" />
              <a-dropdown>
                <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
                <a-menu slot="overlay">
                  <a-menu-item>
                    <a @click="handleDetail(record)">详情</a>
                  </a-menu-item>
                  <a-menu-item>
                    <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                      <a>删除</a>
                    </a-popconfirm>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
            </span>
          </a-table>
        </a-tab-pane>
        <a-tab-pane key="needEdit" tab="需修改">
          <a-button type="primary" icon="download" style="margin-bottom: 5px"
                    @click="handleExportXls('费用申请-需修改')">导出
          </a-button>
          <a-table
            ref="needEditTable"
            size="middle"
            bordered
            :rowKey="
              (record, index) => {
                return record.detailId
              }
            "
            class="j-table-force-nowrap1"
            :scroll="{ x: true }"
            :columns="columns"
            :dataSource="needEditTable.dataSource"
            :pagination="needEditTable.ipagination"
            :loading="needEditTable.loading"
            :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
            @change="handleTableChange"
          >
            <template slot="htmlSlot" slot-scope="text">
              <div v-html="text"></div>
            </template>
            <template slot="imgSlot" slot-scope="text, record">
              <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
              <img
                v-else
                :src="getImgView(text)"
                :preview="record.id"
                height="25px"
                alt=""
                style="max-width: 80px; font-size: 12px; font-style: italic"
              />
            </template>
            <template slot="fileSlot" slot-scope="text">
              <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
              <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
                下载
              </a-button>
            </template>

            <span slot="action" slot-scope="text, record">
              <a v-if="record.status < 0" @click="handleEdit(record)">编辑</a>
              <a-divider type="vertical"></a-divider>
              <act-apply-btn
                v-show="record.status < 0"
                @success="loadData"
                :data-id="record.id"
                :serviceName="serviceName"
                :variables="record"
              ></act-apply-btn>
              <act-historic-detail-btn :data-id="record.id"></act-historic-detail-btn>
              <a-divider type="vertical" />
              <a-dropdown>
                <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
                <a-menu slot="overlay">
                  <a-menu-item>
                    <a @click="handleDetail(record)">详情</a>
                  </a-menu-item>
                  <a-menu-item>
                    <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                      <a>删除</a>
                    </a-popconfirm>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
            </span>
          </a-table>
        </a-tab-pane>
        <a-tab-pane key="approving" tab="审批中">
          <a-button type="primary" icon="download" @click="handleExportXls('费用申请-审批中')">导出</a-button>
          <a-table
            ref="approvingTable"
            size="middle"
            bordered
            :rowKey="
              (record, index) => {
                return record.detailId
              }
            "
            class="j-table-force-nowrap1"
            :scroll="{ x: true }"
            :columns="columns"
            :dataSource="approvingTable.dataSource"
            :pagination="approvingTable.ipagination"
            :loading="approvingTable.loading"
            :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
            @change="handleTableChange"
          >
            <template slot="htmlSlot" slot-scope="text">
              <div v-html="text"></div>
            </template>
            <template slot="imgSlot" slot-scope="text, record">
              <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
              <img
                v-else
                :src="getImgView(text)"
                :preview="record.id"
                height="25px"
                alt=""
                style="max-width: 80px; font-size: 12px; font-style: italic"
              />
            </template>
            <template slot="fileSlot" slot-scope="text">
              <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
              <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
                下载
              </a-button>
            </template>

            <span slot="action" slot-scope="text, record">
              <act-historic-detail-btn :data-id="record.id"></act-historic-detail-btn>
              <a-divider type="vertical" />
              <a-popconfirm  v-if="record.actStatus != '撤回'" title="确定取消申请吗?" @confirm="() => handelSubmitCancel(record)">
               <a href="javascript:;">取消申请</a>
              </a-popconfirm>
              <a-divider type="vertical" />
              <a-dropdown>
                <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
                <a-menu slot="overlay">
                  <a-menu-item>
                    <a @click="handleDetail(record)">详情</a>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
            </span>
          </a-table>
        </a-tab-pane>
        <a-tab-pane key="approved" tab="已审批">
          <a-button type="primary" icon="download" style="margin-bottom: 5px"
                    @click="handleExportXls('费用申请-已审批')">导出
          </a-button>
          <a-table
            ref="approvedTable"
            size="middle"
            bordered
            :rowKey="
              (record, index) => {
                return record.detailId
              }
            "
            class="j-table-force-nowrap1"
            :scroll="{ x: true }"
            :columns="columns"
            :dataSource="approvedTable.dataSource"
            :pagination="approvedTable.ipagination"
            :loading="approvedTable.loading"
            :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
            @change="handleTableChange"
          >
            <template slot="htmlSlot" slot-scope="text">
              <div v-html="text"></div>
            </template>
            <template slot="imgSlot" slot-scope="text, record">
              <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
              <img
                v-else
                :src="getImgView(text)"
                :preview="record.id"
                height="25px"
                alt=""
                style="max-width: 80px; font-size: 12px; font-style: italic"
              />
            </template>
            <template slot="fileSlot" slot-scope="text">
              <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
              <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
                下载
              </a-button>
            </template>

            <span slot="action" slot-scope="text, record">
              <act-historic-detail-btn :data-id="record.id"></act-historic-detail-btn>
              <a-divider type="vertical" />
              <a-dropdown>
                <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
                <a-menu slot="overlay">
                  <a-menu-item>
                    <a @click="handleDetail(record)">详情</a>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
            </span>
          </a-table>
        </a-tab-pane>
      </a-tabs>
    </div>

    <fee-application-modal ref="modalForm" @ok="modalFormOk"></fee-application-modal>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import FeeApplicationModal from './modules/FeeApplicationModal'
import ActApplyBtn from '@views/flowable/components/ActApplyBtn.vue'
import ActHistoricDetailBtn from '@views/flowable/components/ActHistoricDetailBtn.vue'
import { getAction } from '@api/manage'
import moment from 'dayjs'
import { deleteByDataId, stopProcess } from '@views/flowable/api/process'
import { revokeProcess } from '@views/flowable/api/finished'
import { JVXETypes } from '@comp/jeecg/JVxeTable'

export default {
  name: 'FeeApplicationList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    ActHistoricDetailBtn, ActApplyBtn,
    FeeApplicationModal
  },
  data() {
    return {
      description: '费用申请管理页面',
      serviceName: 'feeApplicationService',
      activeKey: 'notGenerated',
      startTime_begin: this.startTime_begin ? `${this.startTime_begin}` : moment().subtract(7, 'days').format('YYYY-MM-DD'),
      startTime_end: this.startTime_end ? `${this.startTime_end}` : moment().format('YYYY-MM-DD'),
      dateFormat: 'YYYY-MM-DD',
      notGeneratedTable: {
        loading: false,
        dataSource: [],
        // 表头
        ipagination: {
          current: 1,
          pageSize: 30,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        }
      },
      needEditTable: {
        loading: false,
        dataSource: [],
        // 表头
        ipagination: {
          current: 1,
          pageSize: 30,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        }
      },
      approvingTable: {
        loading: false,
        dataSource: [],
        // 表头
        ipagination: {
          current: 1,
          pageSize: 30,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        }
      },
      approvedTable: {
        loading: false,
        dataSource: [],
        // 表头
        ipagination: {
          current: 1,
          pageSize: 30,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        }
      },
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '费用申请单号',
          align: 'center',
          dataIndex: 'feeApplicationNum'
        },
        {
          title: '申请人',
          align: 'center',
          dataIndex: 'applyUserName'
        },
        {
          title: '申请部门',
          align: 'center',
          dataIndex: 'applicantDepartment'
        },
        {
          title: '申请日期',
          align: 'center',
          dataIndex: 'applicantDate',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '制单人',
          dataIndex: 'createBy_dictText',
          width: 100
        },
        {
          title: '制单部门',
          dataIndex: 'sysOrgCode',
          width: 150
        },
        {
          title: '制单日期',
          dataIndex: 'createTime',
          width: 150
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/procurement/feeApplication/list',
        delete: '/procurement/feeApplication/delete',
        deleteBatch: '/procurement/feeApplication/deleteBatch',
        exportXlsUrl: '/procurement/feeApplication/exportXls',
        importExcelUrl: 'procurement/feeApplication/importExcel'

      },
      dictOptions: {},
      superFieldList: []
    }
  },
  created() {
    this.getSuperFieldList()
    this.startTime_begin = moment().subtract(7, 'days').format(this.dateFormat)
    this.startTime_end = moment().format(this.dateFormat)
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    searchResetUnCreated() {
      this.startTime_begin = moment().subtract(7, 'days').format(this.dateFormat)
      this.startTime_end = moment().format(this.dateFormat)
      this.loadNotGeneratedData()
    },
    /**  取消流程申请 */
    handelSubmitCancel(row) {
      console.log(row,"test                      ")
      const params = {
        instanceId: row.processInstanceId
      }
      stopProcess(params).then((res) => {
        this.$message.success(res.message)
        this.loadData()
      })
    },
    handleChangeTab() {
      switch (this.activeKey) {
        case 'notGenerated':
          this.loadNotGeneratedData()
          break
        case 'needEdit':
          this.loadNeedEditData()
          break
        case 'approving':
          this.loadApprovingData()
          break
        case 'approved':
          this.loadApprovedData()
          break
        default:
          break
      }
    },
    loadNotGeneratedData() {
      //暂存中，status=-2
      this.tabIndex = 0
      let para = {
        ...this.queryParam,
        status: -2,
        createTimeBg: this.startTime_begin + ' 00:00:00',
        createTimeEd: this.startTime_end + ' 23:59:59'
      }
      this.notGeneratedTable.loading = true
      getAction(this.url.list, para)
        .then((res) => {
          if (res.success) {
            this.notGeneratedTable.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.ipagination.total = res.result.total
            } else {
              this.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.notGeneratedTable.loading = false
        })
    },
    //需修改
    loadNeedEditData() {
      this.tabIndex = 1
      //需修改单据，status=-1
      this.queryParam.status = -1
      this.needEditTable.loading = true
      getAction(this.url.list, this.queryParam)
        .then((res) => {
          if (res.success) {
            this.needEditTable.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.needEditTable.ipagination.total = res.result.total
            } else {
              this.needEditTable.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.needEditTable.loading = false
        })
    },
    //审判中
    loadApprovingData() {
      this.tabIndex = 2
      //审核中单据，status=0
      this.queryParam.status = 0
      this.approvingTable.loading = true
      getAction(this.url.list, this.queryParam)
        .then((res) => {
          if (res.success) {
            this.approvingTable.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.approvingTable.ipagination.total = res.result.total
            } else {
              this.approvingTable.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.approvingTable.loading = false
        })
    },
    //已审批
    loadApprovedData() {
      this.tabIndex = 3
      //已审核单据，status=999
      let para = {
        ...this.queryParam,
        status: 999
      }
      this.approvedTable.loading = true
      getAction(this.url.list, para)
        .then((res) => {
          if (res.success) {
            this.approvedTable.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.approvedTable.ipagination.total = res.result.total
            } else {
              this.approvedTable.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.approvedTable.loading = false
        })
    },
    async loadData() {
      //分别根据单据状态查询结果并将其放在
      this.queryParam = this.getQueryParams()
      this.loadNotGeneratedData()
    },
    initDictConfig() {
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'feeApplicationNum', text: '费用申请单号', dictCode: '' })
      fieldList.push({ type: 'string', value: 'applicant', text: '申请人', dictCode: '' })
      fieldList.push({ type: 'string', value: 'applicantDepartment', text: '申请部门', dictCode: '' })
      fieldList.push({ type: 'date', value: 'applicantDate', text: '申请日期' })
      fieldList.push({ type: 'string', value: 'applicantAmount', text: '申请金额', dictCode: '' })
      fieldList.push({ type: 'string', value: 'approvalProcess', text: '审批流程', dictCode: '' })
      fieldList.push({ type: 'string', value: 'applicantReason', text: '申请原因', dictCode: '' })
      fieldList.push({ type: 'int', value: 'flowFlag', text: '审批流程', dictCode: '' })
      fieldList.push({ type: 'int', value: 'status', text: '状态', dictCode: '' })
      this.superFieldList = fieldList
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>